<?php
/*
	插件页面增删查改
*/
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['plugin_id']){
	$_SESSION['plugin_id']=intval($_RQ['plugin_id']);
}
if(!$_SESSION['plugin_id']){
	exi('请先选择插件！','error');
}
if($_RQ['op']=='list'){
	$params=array('plugin_id'=>$_SESSION['plugin_id']);
	$result=cloud_request('plugin.cpage.list',$params);
	if($result){
			foreach($result['list'] as &$page){
				$page['cpage_op']=array('action'=>'cpage_op','query'=>'page_id='.$page['id'],'title'=>'页面功能设置');
				$page['cpage_event']=array('action'=>'cpage_event','query'=>'page_id='.$page['id'],'title'=>'页面事件管理');
			}
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'页面列表'),
			'1'=>array('op'=>'edit','title'=>'添加页面'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'name','title'=>'页面标识','type'=>'string'),
			array('name'=>'title','title'=>'页面名称','type'=>'string'),
			array('name'=>'cpage_op','title'=>'页面功能设置','type'=>'href','show'=>array('type',array(1,2))),
			array('name'=>'cpage_event','title'=>'页面事件管理','type'=>'href','show'=>array('type',array(1,2))),
			array('op'=>'design','title'=>'设计页面','type'=>'href','show'=>array('type',array(1,2))),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$result['htmls']=$htmls;
	exi($result);
}
if($_RQ['op']=='edit'){
	$params=array('plugin_id'=>$_SESSION['plugin_id']);
	$id=intval($_RQ['id']);
	if($id){
		$params['id']=$id;
	}
	$result=cloud_request('plugin.cpage.edit',$params);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'设置页面信息',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'页面列表'),
			'1'=>array('op'=>'edit','title'=>'添加页面'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'页面标识','name'=>'name', 'help'=>'页面标识，支持字母、数字、大小写，必须以字母开头。且不能和已有功能标识重复' ),
			array('type'=>'string','title'=>'页面名称','name'=>'title' ),
			array('type'=>'switch','title'=>'是否添加权限控制','name'=>'permission' , 'help'=>'如果是管理端页面，设置为是时，操作员访问该页面时，系统会判断操作员是否获得授权。（在添加操作员时可以设置操作员相应权限）</br>如果是用户端页面，设置为是时，用户访问该页面必须先登陆（系统在生成代码时自动添加页面检测代码）'),
			array('type'=>'switch','title'=>'显示入口设置','name'=>'list', 'help'=>'如果设置为是。将在插件首页显示入口设置'),
			array('type'=>'radio','title'=>'页面类型','name'=>'type','options'=>'types', 'help'=>'' ),
			array('type'=>'select','title'=>'指定数据表','name'=>'table','options'=>'tables','show'=>array('type',array(2,3))),
			array(
				'type'=>'array',
				'name'=>'edit_fields',
				'title'=>'表单编辑字段设置',
				'show'=>array('type',3),
				'add_tip'=>'添加一个编辑项',
				'list'=>array(
					array('type'=>'select','name'=>'id','title'=>'字段标识','options'=>'tables[pagedata.item.table].fields','option_title'=>'name'),
					array('type'=>'select','name'=>'id','title'=>'字段名称','options'=>'tables[pagedata.item.table].fields','option_title'=>'title'),
				),'details'=>array(
					array('type'=>'select','name'=>'id','title'=>'选择字段','options'=>'tables[pagedata.item.table][\'fields\']'),
					array('type'=>'select','name'=>'show','title'=>'关联显示','options'=>'tables[pagedata.item.table][\'fields\']','select_title'=>'不关联','option_value'=>'name','help'=>'设置是否关联其他字段值显示。如果关联，则仅当关联字段为指定值时才显示本编辑项'),
					array('type'=>'string','name'=>'show_data','title'=>'关联字段值','help'=>'填写关联字段关联值，多个值以空格隔开。示例：值1 值2，如果设置多个字段值，标识当关联项值为多个值之一时显示本编辑项'),
				)
			),
		)
	);
	$result['htmls']=$htmls;
	exi($result);
}
if($_RQ['op']=='post'){
	$params=array('plugin_id'=>$_SESSION['plugin_id']);
	$post=get_postdata();
	$params['postdata']=base64_encode(json_encode($post));
	$result=cloud_request('plugin.cpage.post',$params);
	exi($result);
}
if($_RQ['op']=='design'){
	//设计页面
	
	$params=array('plugin_id'=>$_SESSION['plugin_id']);
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		
		if($post['items']){
			//图片、音频、视频附件处理
			foreach($post['items'] as &$row){
				foreach($row['params'] as $key=>&$value){
					if(!is_array($value)){
						$type=substr($value,0,7);
						if(in_array($type,array('images/','videos/','audios/')) && is_file(ROOT_AT.$value)){
							$ext=strrchr($value,'.');
							cfc('files')->mkdirs(ROOT_PLUGIN.$_SESSION['plugin_name'].'/custom/resource/'.$_SESSION['page_name']);
							do{
								$file=$_SESSION['plugin_name'].'/custom/resource/'.$_SESSION['page_name'].'/'.random(10).$ext;
							}while(is_file(ROOT_PLUGIN.$file));
							copy(ROOT_AT.$value,ROOT_PLUGIN.$file);
							$value='../{addons}/'.$file;
						}
					}
				}
			}
		}
		//$cs=var_export($post['items']['0'],true);
		//exi($cs,'error');
		$params['postdata']=base64_encode(json_encode($post));
		$result=cloud_request('plugin.cpage.design',$params);
		exi($result);
	}else{
		$result=cloud_request('plugin.cpage.design',$params);
		$_SESSION['page_name']=$result['item']['name'];
		$_SESSION['plugin_name']=$result['plugin_name'];
	}
	exi($result);
}
if($_RQ['op'] == 'del'){
	$params=array('plugin_id'=>$_SESSION['plugin_id']);
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
	}
	if($_RQ['del']){
		$params['del']=trim($_RQ['del']);
	}
	$result=cloud_request('plugin.cpage.del',$params);
	exi($result);
}